/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language enrstuing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;

import me.zhengjie.modules.enrstu.service.AdministrativeDivisionService;
import me.zhengjie.modules.enrstu.service.SchoolDistrictService;
import me.zhengjie.modules.enrstu.service.dto.SchoolDistrictCreateAndUpdateDto;
import me.zhengjie.modules.enrstu.service.dto.SchoolDistrictDto;
import me.zhengjie.modules.enrstu.service.dto.SchoolDistrictQueryCriteria;
import me.zhengjie.modules.enrstu.service.dto.AdministrativeDivisionDto;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @website https://eladmin.vip
* @author yyx
* @date 2024-10-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "招生系统：学区管理")
@RequestMapping("/api/enrstu/schoolDistrict")
@Slf4j
public class SchoolDistrictController {

    private final SchoolDistrictService schoolDistrictService;
    private final AdministrativeDivisionService administrativeDivisionService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('schoolDistrict:list')")
    public void exportSchoolDistrict(HttpServletResponse response, SchoolDistrictQueryCriteria criteria) throws IOException {
        schoolDistrictService.download(schoolDistrictService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询学区管理")
    @ApiOperation("查询学区管理")
    @PreAuthorize("@el.check('schoolDistrict:list')")
    public ResponseEntity<PageResult<SchoolDistrictDto>> querySchoolDistrict(SchoolDistrictQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(schoolDistrictService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("/createOrUpdate")
    @Log("新增或者修改学区管理")
    @ApiOperation("新增或者修改学区管理")
    @PreAuthorize("@el.check('schoolDistrict:add')")
    public ResponseEntity<Object> createOrUpdate(  @Validated @ModelAttribute(value = "resources") SchoolDistrictCreateAndUpdateDto resources
            ,@RequestPart(value = "file",required = false) MultipartFile file){
        schoolDistrictService.createOrUpdate(resources,file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }




    @DeleteMapping("/delete/{id}/{isDeleteSdId}")
    @Log("删除学区管理")
    @ApiOperation("删除学区管理")
    @PreAuthorize("@el.check('schoolDistrict:del')")
    public ResponseEntity<Object> deleteSchoolDistrict(@PathVariable String id, @PathVariable Boolean isDeleteSdId) {
        schoolDistrictService.delete(id,isDeleteSdId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/query/province")
    @Log("获取全国省列表")
    @ApiOperation("获取省列表")
    @PreAuthorize("@el.check('schoolDistrict:list')")
    public ResponseEntity<Object> queryProvince(@RequestParam(required = false) Long administrativeDivisionId) {
        List<AdministrativeDivisionDto> administrativeDivisionList;
        //administrativeDivisionId为空，查询所有省列表
        if (administrativeDivisionId == null) {
            administrativeDivisionList = administrativeDivisionService.findByDivisionLevel(10);
        }else {
            //administrativeDivisionId不为空，查询该行政区的下级行政区
            administrativeDivisionList = administrativeDivisionService.findByIdsublevelList(administrativeDivisionId);
        }
        return new ResponseEntity<>(administrativeDivisionList,HttpStatus.OK);
    }
    /**
     * exportAD：行政划分区导出
     * AD:Administrative divisions :行政划分区
     */
    @Log("行政划分区模版下载")
    @ApiOperation("行政划分区模版下载")
    @GetMapping(value = "/download/ad")
    @PreAuthorize("@el.check('schoolDistrict:list')")
    public void  exportAD(HttpServletResponse response
    ) throws IOException {
        schoolDistrictService.download(response);
    }

}
